#coding=utf-8
'''
Created on Aug 2, 2016

@author: wangpeng
'''
import unittest
from appium import webdriver
from selenium.webdriver.common.by import By   #用于匹配某个值是否出现，智能等待
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import common_setting.logging_save
import common_setting.desired_setting
import common_setting.config
import common_setting.Create_Log_Folder
import common_setting.labview_python_connect
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import os
import choose_mobile
# 测试内容
# 1.DUT进入配对模式，2.手机搜索进行配对连接，3.手机取消配对连接，4.手机再次搜索进行配对连接

class Test(unittest.TestCase):
    def setUp(self):
        os.system('taskkill /f /im adb.exe')
        common_setting.logging_save.logging.info("app测试开始，打开kugou，手机调节音量")
        print time.ctime(),"app测试开始，打开kugou，进行暂停播放操作"
        self.save_log = common_setting.Create_Log_Folder.Create('\\basic_function\\adjust_vol_mobile')    #建立report保存路径
        self.desired_setting = common_setting.desired_setting.desired_setting()
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', self.desired_setting)
        time.sleep(10)
        self.driver.quit()

    def tearDown(self):
        self.driver.quit()
        time.sleep(15)
        common_setting.logging_save.logging.info("此项测试结束")
        print time.ctime(),'此项测试结束'


    def testName(self):
        u"""测试步骤： 1.打开酷狗  2.手机端调节音量  3.判断声音是否有输出  预期结果：play状态有声音检测输出和正常"""
        savenumber = 1
        self.save_pic = os.path.join(self.save_log+str(savenumber)+'.png')
        #确认名字是否确认，然后进行连接
        if choose_mobile.want_to_test_mobile == 'huawei_honor7':
            import common_setting.cell_function.huawei_mobile_entry_BT    #不同手机调用不同的配置
            name_detect = common_setting.cell_function.huawei_mobile_entry_BT.name_detect
            print name_detect
            time.sleep(5)

        else:
            pass
        time.sleep(5)
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', self.desired_setting)
        common_setting.logging_save.logging.info("重新进入酷狗")
        print time.ctime(),"重新进入酷狗"
        time.sleep(10)
        
        self.driver.find_element_by_id(common_setting.config.kugou_menu_play_pause()).click() #点击播放歌曲
        time.sleep(8)

        #手机端调节音量，安卓调节
        for down in range(5):
            self.driver.keyevent(25)       
            common_setting.logging_save.logging.info("手机降低一格音量")
            print time.ctime(),"手机降低一格音量"
            time.sleep(0.5)
        
        for down in range(10):
            self.driver.keyevent(24)       
            common_setting.logging_save.logging.info("手机增加一格音量")
            print time.ctime(),"手机增加一格音量"    
            time.sleep(0.5)
                               
        common_setting.labview_python_connect.write_log('PLAY ', '10')
        time.sleep(15)
        
        ftest = open(common_setting.config.labview_result(),'r')
        time.sleep(2)
        test_result = ftest.readline().strip()
        time.sleep(1)
        ftest.close()
        time.sleep(3)
        print test_result
        
        if test_result == 'OK':
            common_setting.logging_save.logging.info("检测到播放有声音")
            print time.ctime(),"检测到播放有声音"
        
        elif test_result == 'NG':
            common_setting.logging_save.logging.info("声音播放有卡顿现象")
            print time.ctime(),"声音播放有卡顿现象"
            self.driver.get_screenshot_as_file(self.save_pic)
            
        else:
            common_setting.logging_save.logging.info("检测到没有声音，请确认问题所在")
            print time.ctime(),"检测到没有声音，请确认问题所在"
            self.driver.get_screenshot_as_file(self.save_pic)
            
if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()